﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmKetQuaHocTap_GiangVien : FormBase
    {
        public frmKetQuaHocTap_GiangVien()
        {
            InitializeComponent();
        }

        public int NamThucTapID { get; set; }
        public int GiangVienID { get; set; }
        private DataTable _dtLoad;

        private void frmKetQuaHocTap_GiangVien_Load(object sender, EventArgs e)
        {
            try
            {
                _dtLoad = new DataTable();
                _dtLoad.Columns.Add("SinhVienID", typeof(string));
                _dtLoad.Columns.Add("MaSinhVien", typeof(string));
                _dtLoad.Columns.Add("Ho", typeof(string));
                _dtLoad.Columns.Add("Ten", typeof(string));
                _dtLoad.Columns.Add("Diem", typeof(double));
                _dtLoad.Columns.Add("NhanXet", typeof(string));

                LoadSinhVien();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (CommonConfirm("Bạn muốn thêm dữ liệu này?", "LUU"))
                {
                    using (NestedUnitOfWork nuow = Session.DefaultSession.BeginNestedUnitOfWork())
                    {
                        GiangVienTrongNam objGiangVien = nuow.Query<GiangVienTrongNam>().FirstOrDefault(c => c.Oid == GiangVienID);
                        if (objGiangVien != null)
                        {
                            for (int i = 0; i < _dtLoad.Rows.Count; i++)
                            {
                                DataRow dr = _dtLoad.Rows[i];
                                int sinhvienID = Convert.ToInt32(dr["SinhVienID"]);
                                var objSinhVien = nuow.Query<SinhVienTrongGiangVien>().FirstOrDefault(c => c.Oid == sinhvienID);
                                if (objSinhVien != null)
                                {
                                    objSinhVien.Diem = Convert.ToDouble(dr["Diem"]);
                                    objSinhVien.NhanXet = dr["NhanXet"].ToString();
                                }
                            }
                            nuow.CommitChanges();
                        }
                    }
                    CommonMessage("Cập nhật thành công");
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void LoadSinhVien()
        {
            var query = Session.DefaultSession.Query<SinhVienTrongGiangVien>().Where(c => c.GiangVienTrongNam.Oid == GiangVienID).Select(c => new
            {
                c.Oid,
                c.SinhVien.MaSinhVien,
                c.SinhVien.HoSinhVien,
                c.SinhVien.TenSinhVien,
                c.Diem,
                c.NhanXet
            });
            foreach (var item in query)
            {
                DataRow dr = _dtLoad.NewRow();
                dr["SinhVienID"] = item.Oid;
                dr["MaSinhVien"] = item.MaSinhVien;
                dr["Ho"] = item.HoSinhVien;
                dr["Ten"] = item.TenSinhVien;
                dr["Diem"] = item.Diem;
                dr["NhanXet"] = item.NhanXet;
                _dtLoad.Rows.Add(dr);
            }
            gvCurrent.DataSource = _dtLoad;
        }
    }
}